package Factory.Entities;

import Model.Core.ICommunicatorEntity;
import Model.Core.Impl.LinkEntity;
import Model.Core.Impl.ObjectEntity;
import Model.Draw.Entities.Impl.LinkLine;
import Model.Draw.Entities.LinkDraw;
import Model.Draw.Entities.ObjectDraw;
import Model.Draw.Events.ManifestEntityDoneListener;
import Model.Draw.Events.ManifestElementEvent;
import Model.Draw.ICommunicatorDraw;
import java.util.ArrayList;
import java.util.List;

/**
 * Projeto de TCC: Editor de Diagramas de comunicação BlueJ
 * @author Jose
 */
public class LinkFactory {
    private List<ManifestEntityDoneListener> listeners;
    
    public LinkFactory(){
        listeners = new ArrayList<ManifestEntityDoneListener>();
    }
    
    public void addListener(ManifestEntityDoneListener listener){
        if(!listeners.contains(listener))
            this.listeners.add(listener);
    }
    
    public void removeListener(ManifestEntityDoneListener listener){
        this.listeners.remove(listener);
    }
    
    public void createEntityGraphicalManifest(LinkEntity linkEntity, Object sourceElement, Object targetElement){
        ICommunicatorDraw sourceDraw = (ICommunicatorDraw)sourceElement;
        ObjectDraw targetDraw = (ObjectDraw)targetElement;
        linkEntity.setObjectSource((ICommunicatorEntity)sourceDraw.getEntity());
        linkEntity.setObjectTarget((ObjectEntity)targetDraw.getEntity());
        LinkDraw graphicalManifest = new LinkDraw(new LinkLine(sourceDraw.findNearstJointOf(targetDraw), targetDraw.findNearstJointOf(sourceDraw)), sourceDraw, targetDraw);
        graphicalManifest.setEntity(linkEntity);
        notifyListeners(graphicalManifest);
    }

    private void notifyListeners(LinkDraw graphicalManifest) {
        for(ManifestEntityDoneListener listener : listeners){
            listener.manifestEntityDoneHandler(new ManifestElementEvent(graphicalManifest));
        }
    }
}
